package es.buffygilmore.collectionapp;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ElementoList extends ListActivity {

	private static final int ACTIVITY_CREATE_ELEMENT = 0;
	private static final int ACTIVITY_EDIT_ELEMENT = 1;
	private static final int ACTIVITY_EDIT_COLLECTION = 2;

	private CollectionAppDbAdapter mDbHelper;
	private Long mIdColeccion;

	private ImageView mImageView;
	private ImageView addButton;
	private ImageView editButton;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.listado_elementos);
		this.mDbHelper = new CollectionAppDbAdapter(this);
		this.mDbHelper.open();

		this.mImageView = (ImageView) this.findViewById(R.id.iconoElemento1);

		this.addButton = (ImageView) this.findViewById(R.id.botonAdd);
		this.editButton = (ImageView) this.findViewById(R.id.botonEditarColeccion);

		this.mIdColeccion = (savedInstanceState == null) ? null : (Long) savedInstanceState
				.getSerializable(CollectionAppDbAdapter.KEY_COLECCION_ROWID);

		if (this.mIdColeccion == null) {
			final Bundle extras = this.getIntent().getExtras();
			this.mIdColeccion = extras != null ? extras
					.getLong(CollectionAppDbAdapter.KEY_COLECCION_ROWID) : null;

			final String tituloColeccion = extras != null ? extras
					.getString(CollectionAppDbAdapter.KEY_COLECCION_TITULO) : null;
			if (tituloColeccion != null) {
				// Recupero el textview
				final TextView tituloTextView = (TextView) this
						.findViewById(R.id.elemento_titulo_coleccion);
				tituloTextView.setText(tituloColeccion);
			}

		}

		this.fillElementosData();
		this.registerForContextMenu(this.getListView());

		this.addButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View view) {
				ElementoList.this.addButton.setColorFilter(0xEEBD8ED7, PorterDuff.Mode.MULTIPLY);
				ElementoList.this.crearElemento();
			}

		});

		this.editButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View view) {
				ElementoList.this.editButton.setColorFilter(0xEEBD8ED7, PorterDuff.Mode.MULTIPLY);
				ElementoList.this.modificarElemento();
			}

		});

	}

	protected void modificarElemento() {
		final Intent i = new Intent(this, ColeccionEdit.class);
		i.putExtra(CollectionAppDbAdapter.KEY_COLECCION_ROWID, this.mIdColeccion);
		this.startActivityForResult(i, ACTIVITY_EDIT_COLLECTION);

	}

	private void fillElementosData() {
		// Get all of the rows from the database and create the item list TODO
		final Cursor elementosCursor = this.mDbHelper
				.obtenerElementosPorColeccion(this.mIdColeccion);
		this.startManagingCursor(elementosCursor);

		// Create an array to specify the fields we want to display in the list (only TITLE)
		final String[] from = new String[] { CollectionAppDbAdapter.KEY_ELEMENTO_NOMBRE,
				CollectionAppDbAdapter.KEY_ELEMENTO_COMENTARIO };

		// and an array of the fields we want to bind those fields to (in this case just text1)
		final int[] to = new int[] { R.id.elemento1, R.id.elemento2 };

		// Now create a simple cursor adapter and set it to display
		final SimpleCursorAdapter elementos = new SimpleCursorAdapter(this, R.layout.fila_elemento,
				elementosCursor, from, to);
		this.setListAdapter(elementos);

		// Rellenamos el total
		final TextView numeroElementosTextView = (TextView) this
				.findViewById(R.id.numero_elementos);
		final int totalElementos = elementosCursor.getCount();
		numeroElementosTextView.setText(String.valueOf(totalElementos));

	}

	private void crearElemento() {
		final Intent i = new Intent(this, ElementoEdit.class);
		i.putExtra(CollectionAppDbAdapter.KEY_ELEMENTO_COLECCION, this.mIdColeccion);
		this.startActivityForResult(i, ACTIVITY_CREATE_ELEMENT);
	}

	@Override
	protected void onListItemClick(final ListView l, final View v, final int position, final long id) {
		super.onListItemClick(l, v, position, id);
		final Intent i = new Intent(this, ElementoEdit.class);
		i.putExtra(CollectionAppDbAdapter.KEY_ELEMENTO_ROWID, id);
		i.putExtra(CollectionAppDbAdapter.KEY_ELEMENTO_COLECCION, this.mIdColeccion);
		this.startActivityForResult(i, ACTIVITY_EDIT_ELEMENT);
	}

	@Override
	protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		this.addButton.clearColorFilter();
		this.editButton.clearColorFilter();
		this.fillElementosData();
	}

}
